//answer.js
var util = require('../../utils/util.js')

var app = getApp()
Page({
  data: {

    flag: 1,
    Focus: '+关注'
  },

  // ---------------------数据加载模块 start---------------
  onLoad: function (options) {
    var qid = options.qid;
    var qTitle = options.qTitle;
    var qContent = options.qContent;
    var qFocusNum = options.qFocusNum;
    //console.log("qContent:"+qContent)
    this.setData({
      qid: qid,
      qTitle: qTitle,
      qContent: qContent,
      qFocusNum: qFocusNum,
    })
    // console.log("qid:"+qid);

    this.getAnswerByqid();

  },
  // ---------------------数据加载模块 end---------------


  // ---------------------功能模块 start---------------
  getAnswerByqid: function () {
    var mark = 'getAnswerByqid';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data: {
        mark: mark,
        qid: this.data.qid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res) => {
        var aList = res.data.data.aList;
        this.setData({
          answerList: aList,
        })
        // console.log("关注页面")
        // console.log(this.data.qFocusNum)
      }
    })
  },


  //关注按钮的状态转变及关注数的增减
  focus: function () {
    if (this.data.flag) {
      var mark = 'updateFocusNumByqid';
      wx.request({
        url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
        method: 'POST',
        data: {
          mark: mark,
          qid: this.data.qid,
          qFocusNum:this.data.qFocusNum
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json'
        },
        success: (res) => {
          
    var mark = 'getFocusNumByqid';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data: {
        mark: mark,
        qid: this.data.qid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res) => {
        var qFocusNum = res.data.data.question;
        this.setData({
          flag: 0,
          Focus: '已关注',
          qFocusNum:qFocusNum
        })
        // console.log("关注页面")
        // console.log(this.data.qFocusNum)
      }
    })

        }
      })
    } else {
      var mark = 'updateFocusNumByqid01';
      wx.request({
        url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
        method: 'POST',
        data: {
          mark: mark,
          qid: this.data.qid,
          qFocusNum:this.data.qFocusNum
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json'
        },
        success: (res) => {
          
    var mark = 'getFocusNumByqid';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data: {
        mark: mark,
        qid: this.data.qid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res) => {
        var qFocusNum = res.data.data.question;
        this.setData({
          flag: 1,
          Focus: '+关注',
          qFocusNum:qFocusNum
        })
        // console.log("关注页面")
        // console.log(this.data.qFocusNum)
      }
    })

        }
      })
    }
    // console.log(this.data.flag)
  },
  // ---------------------功能模块 end---------------


  // ---------------------页面跳转模块 start---------------
  // 跳转到写回答页面
  bindQueTap: function () {
    wx.navigateTo({
      url: '../write_answer/write_answer'
    })
  },

  goAnswer: function (e) {
    var answer = e.currentTarget.dataset.answer
    var qTitle = this.data.qTitle;
    //console.log(answer);
    var answerJson = JSON.stringify(answer);
    wx.navigateTo({
      url: '../answer/answer?answer=' + answerJson + '&qTitle=' + qTitle
    })
  },
  // ---------------------页面跳转模块 end---------------
})